package pl.drypisy;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * route list adapter 
 * uses RouteData (int chId, String title,String date, String about)
 * @author Michal Drobniak
*/
class RouteListAdapter extends ArrayAdapter<RouteData> {
	private ArrayList<RouteData> routes;
	private LayoutInflater vi;

	public RouteListAdapter(Context context, int textViewResourceId, ArrayList<RouteData> routes, LayoutInflater vi) {
		super(context, textViewResourceId, routes);
		this.routes=routes;
		this.vi=vi;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		View v=convertView;
		if(v==null){
           v = vi.inflate(R.layout.route_list, null);
		}
		
		RouteData route=routes.get(position);
		if(route!=null){
			TextView nr=(TextView) v.findViewById(R.id.routeNr);
			TextView title=(TextView) v.findViewById(R.id.routeTitle);
			TextView date=(TextView) v.findViewById(R.id.routeDate);
			TextView slicedAbout=(TextView) v.findViewById(R.id.routeSlicedAbout);
			TextView routeAboute=(TextView) v.findViewById(R.id.routeAbout);
			
			String slicedAboutText="";
			if(route.about.length()>40)
				slicedAboutText=route.about.substring(0, 40) + "..";
			else slicedAboutText=route.about;
			
			if(nr!=null) nr.setText(route.id+"");
			if(title!=null) title.setText(route.title);
			if(date!=null) date.setText(route.date);
			if(slicedAbout!=null) slicedAbout.setText(slicedAboutText);
			if(routeAboute!=null) routeAboute.setText(route.about);
		}
		v.setTag(route.id); //view's tag with route id, read in onItemClickListener
	return v;
	}
}
